package com.atguigu.tingshu.user.api;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.common.login.UserLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.UserPaidTrackService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private UserPaidTrackService userPaidTrackService;

	@GetMapping("updateVipExpireStatus")
	public Result updateVipExpireStatus() {
		userInfoService.updateVipExpireStatus();
		return Result.ok();
	}

	@GetMapping("/wxLogin/getUserInfo")
	@UserLogin(required = true)
	public Result<UserInfoVo> getUserInfo(){
		Long userId = AuthContextHolder.getUserId();
		UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
		return Result.ok(userInfoVo);
	}


	@PostMapping("/wxLogin/updateUser")
	@UserLogin(required = true)
	public Result updateUser(@Validated @RequestBody UserInfoVo userInfoVo){
		Long userId = AuthContextHolder.getUserId();
		userInfoService.updateUser(userId,userInfoVo);
		return Result.ok();
	}

	/**
	 * 根据用户ID获取用户（主播）基本信息
	 *
	 * @param userId
	 * @return
	 */
	@GetMapping("/userInfo/getUserInfoVo/{userId}")
	public Result<UserInfoVo> getUserInfoVoById(@PathVariable Long userId) {
		UserInfoVo userInfoVo = userInfoService.getUserInfoVoById(userId);
		return Result.ok(userInfoVo);
	}


	/**
	 * 获取用户声音列表付费情况
	 * @param userId
	 * @param albumId
	 * @return
	 */
	@PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
	public Result<Map<String,Integer>> getUserIsPaidTrack(@PathVariable Long userId, @PathVariable Long albumId,@RequestBody List<Long> needCheckTrackIdList){
		Map<String,Integer> map = userPaidTrackService.getUserIsPaidTrack(userId,albumId,needCheckTrackIdList);
		return Result.ok(map);
	}

	/**
	 * 提供给订单服务调用，验证当前用户是否购买过专辑
	 * @param albumId
	 * @return
	 */
	@UserLogin
	@GetMapping("/userInfo/isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId){
		Long userId = AuthContextHolder.getUserId();
		Boolean isBuy = userInfoService.isPaidAlbum(userId,albumId);
		return Result.ok(isBuy);
	}


	/**
	 * 提供给专辑服务调用，获取当前用户已购声音集合
	 *
	 * @param albumId
	 * @return
	 */
	@UserLogin
	@GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
	public Result<List<Long>> getUserPaidTrackIdList(@PathVariable Long albumId) {
		Long userId = AuthContextHolder.getUserId();
		List<Long> userPaidTrackIdList = userInfoService.getUserPaidTrackIdList(userId, albumId);
		return Result.ok(userPaidTrackIdList);
	}

	@UserLogin
	@PostMapping("/userInfo/savePaidRecord")
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
		Long userId = AuthContextHolder.getUserId();
		userInfoService.savePaidRecord(userId,userPaidRecordVo);
		return Result.ok();
	}
}

